// hyb_yl/mysubpages/pages/docorder/docorder.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabitem: ['待接诊', '已接诊', '已结束', '退款'],
    docindex: 0,
    key_words:"",
    page:0,
    list:[],
  },
  doctab: function (e) {
    var that = this
    var key_words = e.currentTarget.dataset.key_words
    
    that.setData({
      key_words: key_words,
      page:0,
      list:[]
    })
    that.getOrder();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  lookdetail: function (e) {
    var id = e.currentTarget.dataset.id;
    
    var that = this
    var list = e.currentTarget.dataset
    var orders = e.currentTarget.dataset.orders
    var j_id = e.currentTarget.dataset.j_id
    var key_words = that.data.key_words
    console.log(key_words)
    var zid = e.currentTarget.dataset.zid
    var ifpay = e.currentTarget.dataset.ifpay
    var money = e.currentTarget.dataset.money
    var overtime = e.currentTarget.dataset.overtime
    var id = e.currentTarget.dataset.id
    var ifgb = e.currentTarget.dataset.ifgb
    wx.setStorage({
      key: 'userinfotype',
      data: 'user',
    })
    let userInfo = wx.getStorageSync('userInfo')
    console.log(userInfo)
    let bl_id = e.currentTarget.dataset.bl_id
    wx.setStorage({
      key: 'userInfo',
      data: userInfo,
    })
    let z_thumbs = e.currentTarget.dataset.z_thumbs //医生头像
    wx.setStorage({
      key: 'z_thumbs',
      data: z_thumbs,
    })
    let sate = 0
    wx.setStorageSync('sate', sate) //证明是用户发送的信息
    wx.setStorageSync('docroom', your)
    var my = e.currentTarget.dataset.myroom; //自己
    var your = e.currentTarget.dataset.docroom; //医生
    var dex = e.currentTarget.dataset.dex;
    wx.setStorageSync('docroom', your)
    var nameList = {
      myName: my,
      your: your,
      bl_id: bl_id
    };
    if (ifpay == 5) {
      wx.showToast({
        title: '退款申请中无法查看详情',
        icon: 'none'
      })
      return false;
    }
    if (ifpay == 6) {
      wx.showToast({
        title: '已退款',
        icon: 'none'
      })
      return false;
    }

    wx.navigateTo({
      url: '/hyb_yl/zhuanjiasubpages/pages/zhifuend/zhifuend?txt=yes&zid=' + zid + '&back_orser=' + orders + '&key_words=yuanchengguahao' + '&j_id=' + j_id + '&ifpay=' + ifpay + '&money=' + money + '&overtime=' + overtime + '&id=' + id + '&ifgb=' + ifgb,
    })

  },
  zixunxq(e) {
    var that = this
    console.log(e)
    var list = e.currentTarget.dataset
    var orders = e.currentTarget.dataset.orders
    var j_id = e.currentTarget.dataset.j_id
    var key_words = that.data.key_words
    console.log(key_words)
    var zid = e.currentTarget.dataset.zid
    var ifpay = e.currentTarget.dataset.ifpay
    var money = e.currentTarget.dataset.money
    var overtime = e.currentTarget.dataset.overtime
    var ifgb = e.currentTarget.dataset.ifgb
    wx.setStorage({
      key: 'userinfotype',
      data: 'user',
    })
    let userInfo = wx.getStorageSync('userInfo')
    console.log(userInfo)
    let bl_id = e.currentTarget.dataset.bl_id
    wx.setStorage({
      key: 'userInfo',
      data: userInfo,
    })
    let z_thumbs = e.currentTarget.dataset.z_thumbs //医生头像
    wx.setStorage({
      key: 'z_thumbs',
      data: z_thumbs,
    })
    let sate = 0
    wx.setStorageSync('sate', sate) //证明是用户发送的信息
    wx.setStorageSync('docroom', your)
    var my = e.currentTarget.dataset.myroom; //自己
    var your = e.currentTarget.dataset.docroom; //医生
    var dex = e.currentTarget.dataset.dex;
    wx.setStorageSync('docroom', your)
    var nameList = {
      myName: my,
      your: your,
      bl_id: bl_id
    };
    console.log(e)
    var that = this
    
    if (ifpay == 5) {
      wx.showToast({
        title: '退款申请中无法查看详情',
        icon: 'none'
      })
      return false;
    }
    if (ifpay == 6) {
      wx.showToast({
        title: '已退款',
        icon: 'none'
      })
      return false;
    }
    if (key_words == 'tuwenwenzhen') {
      wx.navigateTo({
        url: '/hyb_yl/czhuanjiasubpages/pages/chatroom/chatroom?username=' + JSON.stringify(nameList) + '&list=' + JSON.stringify(list) + '&z_name=' + e.currentTarget.dataset.z_name + '&orders=' + orders + '&dex=' + dex + '&key_words=' + key_words + '&ifpay=' + ifpay + '&money=' + money + '&overtime=' + overtime +'&ifgb=' + ifgb + '&txt=yes',
      })
    }
    if (key_words == 'dianhuajizhen' || key_words == 'shipinwenzhen' || key_words == 'shoushukuaiyue') {
      wx.navigateTo({
        url: '/hyb_yl/zhuanjiasubpages/pages/zhifuend/zhifuend?txt=yes&zid=' + zid + '&back_orser=' + orders + '&key_words=' + key_words + '&j_id=' + j_id + '&ifpay=' + ifpay + '&money=' + money + '&overtime=' + overtime + '&ifgb=' + ifgb,
      })
    }
    if (key_words == "yuanchengkaifang") {
      var c_id = e.currentTarget.dataset.c_id
      wx.navigateTo({
        url: '/hyb_yl/zhuanjiasubpages/pages/zhifuend/zhifuend?txt=yes&zid=' + zid + '&back_orser=' + orders + '&key_words=' + key_words + '&j_id=' + j_id + '&ifpay=' + ifpay + '&money=' + money + '&overtime=' + overtime + '&ifgb=' + ifgb + '&c_id=' + c_id,
      })
    }
    if (key_words == "shoushukuaiyue") {
      wx.navigateTo({
        url: '/hyb_yl/zhuanjiasubpages/pages/zhifuend/zhifuend?txt=yes&zid=' + zid + '&back_orser=' + orders + '&key_words=' + key_words + '&j_id=' + j_id + '&ifpay=' + ifpay + '&money=' + money + '&overtime=' + overtime + '&ifgb=' + ifgb,
      })
    }
  },
  onLoad: function (options) {
    var that = this;
    var key_words = options.keyword
    if(key_words != '' && key_words != undefined)
    {
      that.setData({
        key_words: key_words,
      })
    }
    console.log(that.data.key_words)
    
    wx.setNavigationBarTitle({
      title: '机构订单'
    })
    that.getServer();
    that.getOrder();
    
  },
  // 获取服务
  getServer:function(){
    var that = this;
    app.util.request({
      'url':"entry/wxapp/regin.server_list",
      success:function(res){
        that.setData({
          tabitem:res.data,
         
        })
        if(that.data.key_words == '')
        {
          that.setData({
            key_words:res.data[0]['key_words']
          })
        }
        
      }
    })
  },
  // 获取订单
  getOrder:function(){
    var that = this;
    var key_words = that.data.key_words;
    app.util.request({
      'url':"entry/wxapp/regin.hospital_order",
      data:{
        page:that.data.page,
        key_words:key_words,
        openid:wx.getStorageSync('openid'),
      },
      success:function(res){
        var page = that.data.page + 1;
        that.setData({
          page:page,
          list:that.data.list.concat(res.data)
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getOrder()
  },
  getchar(e) {
    var that = this
    var id = e.currentTarget.dataset.id
    var zid = e.currentTarget.dataset.zid
    var typedate = e.currentTarget.dataset.typedate
    var key_words = that.data.key_words
    var j_id = e.currentTarget.dataset.j_id
    var back_orser = e.currentTarget.dataset.back_orser
    var openid = e.currentTarget.dataset.openid
    var overtime = e.currentTarget.dataset.overtime
    var ifpay = e.currentTarget.dataset.ifpay
    
    wx.navigateTo({
      url: '/hyb_yl/zhuanjiasubpages/pages/zhuanjiahuida/zhuanjiahuida?id=' + id + '&zid=' + zid + '&typedate=' + typedate + '&key_words=' + key_words + '&j_id=' + j_id + '&back_orser=' + back_orser + '&openid=' + openid + '&overtime=' + overtime + '&ifpay=' + ifpay,
    })
  },
  joinm(e) {
    console.log(e)
    var roomID = e.currentTarget.dataset.roomid
    var sdkAppID = e.currentTarget.dataset.sdkappid
    var template = e.currentTarget.dataset.template
    var userID = e.currentTarget.dataset.userid
    var id = e.currentTarget.dataset.id
    var userSig = e.currentTarget.dataset.usersig
    var orders = e.currentTarget.dataset.orders
    app.util.request({
      'url': 'entry/wxapp/zhuanjia.updatesporder',
      data: {
        orders: orders
      },
      success: function (res) {
        console.log(res)
      }
    })
    var url = "/hyb_yl/webrtc-room/room/room?roomID=" + roomID + '&template=' + template + '&sdkAppID=' + sdkAppID + '&userId=' + userID + '&userSig=' + userSig + '&id=' + id + '&typeindex=1';
    wx.navigateTo({
      url: url,
    })
  },
  entrec(e) {
    var that = this
    var z_telephone = e.currentTarget.dataset.z_telephone
    var j_id = e.currentTarget.dataset.j_id
    var privateNum = e.currentTarget.dataset.private
    var back_orser = e.currentTarget.dataset.back_orser
    var zid = e.currentTarget.dataset.zid
    app.util.request({
      'url': 'entry/wxapp/yuyue.setaxndel',
      data: {
        doc_phone: z_telephone,
        back_orser: back_orser,
        privateNum: privateNum
      },
      success: function (res) {
        console.log(res)
        if (res.data.resultcode == '1016001') {
          wx.showToast({
            title: '您已拨打过电话',
          })
        } else {
          if (res.data.resultdesc == 'Success') {
            wx.makePhoneCall({
              phoneNumber: privateNum,
              success: function () {
                console.log("拨打电话成功！")
                app.util.request({
                  'url': 'entry/wxapp/zhuanjia.updatesporder',
                  data: {
                    orders: back_orser
                  },
                  success(res) {
                    console.log(res)
                  }
                })
              },
              fail: function () {
                console.log("拨打电话失败！")
              },
              complete: function () {
                //执行解绑
                // setTimeout(function () {
                //   app.util.request({
                //     'url': 'entry/wxapp/yuyue.uphuawtel',
                //     data: {
                //       doc_phone: z_telephone,
                //       j_id: j_id,
                //       privateNum: privateNum,
                //       back_orser: back_orser,
                //       zid: zid
                //     },
                //     success: function (res) {
                //       console.log(res)
                //       //成功后改变状态
                //     }
                //   })
                // }, 40000)
              }
            })
          }
        }

      }
    })
  },
  twentrec(e) {
    var that = this
    var j_id = e.currentTarget.dataset.j_id
    var back_orser = e.currentTarget.dataset.back_orser
    var zid = e.currentTarget.dataset.zid
    var key_words = that.data.key_words
    var id = e.currentTarget.dataset.id
    var openid = e.currentTarget.dataset.openid
    wx.showModal({
      title: '提示',
      content: '是否接诊',
      success(res) {
        if (res.confirm) {
          app.util.request({
            'url': 'entry/wxapp/zhuanjia.updatetwstate',
            data: {
              back_orser: back_orser
            },
            success: function (res) {
              console.log(res)
              app.util.request({
                'url': 'entry/wxapp/zhuanjia.tuwenmsgdh',
                data: {
                  id: id,
                },
                success: function (res) {
                  console.log(res)
                }
              })
              var overtime = res.data.overtime
              wx.navigateTo({
                url: '/hyb_yl/zhuanjiasubpages/pages/zhuanjiahuida/zhuanjiahuida?id=' + id + '&zid=' + zid + '&key_words=' + key_words + '&j_id=' + j_id + '&back_orser=' + back_orser + '&openid=' + openid + '&overtime=' + overtime,
              })
            }
          })
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  chufangentr(e) {
    var that = this
    var j_id = e.currentTarget.dataset.j_id
    var back_orser = e.currentTarget.dataset.back_orser
    var zid = e.currentTarget.dataset.zid
    var key_words = that.data.key_words
    var id = e.currentTarget.dataset.id
    var openid = e.currentTarget.dataset.openid
    wx.showModal({
      title: '提示',
      content: '是否接诊',
      success(res) {
        if (res.confirm) {
          app.util.request({
            'url': 'entry/wxapp/zhuanjia.updatecfstate',
            data: {
              back_orser: back_orser
            },
            success: function (res) {
              console.log(res)
              app.util.request({
                'url': 'entry/wxapp/zhuanjia.kfjzmsgdh',
                data: {
                  id: id,
                },
                success: function (res) {
                  console.log(res)
                }
              })
              var overtime = res.data.overtime
              wx.navigateTo({
                url: '/hyb_yl/zhuanjiasubpages/pages/zhuanjiahuida/zhuanjiahuida?id=' + id + '&zid=' + zid + '&key_words=' + key_words + '&j_id=' + j_id + '&back_orser=' + back_orser + '&openid=' + openid + '&overtime=' + overtime,
              })
            }
          })
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
})